<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2019/3/14
 * Time: 16:40
 */

namespace app\admin\controller;
use app\admin\model\Rule;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Common extends Controller{
    public function __construct(Request $request=null){
        parent::__construct($request);
        if (Session::has("admin_id")==false){
            $this->error("请登录再访问","login/index");
        }
        $admin_id=Session::get("admin_id");
        //如果当前登录用户为超级管理员账号，则不做权限验证
        $admininfo=Session::get('admin');
        if($admininfo['is_super']==1){
            $model=new Rule();
            $field='id,name,title,pid';
            $data=$model->getAll(['status'=>1],$field);
            $this->rules=node_merge($data);
            return;
        }

        $data=Db::name("admin_role")->alias('a')
            ->join('role_rule r', 'a.role_id=r.role_id')
            ->join('rule u', 'r.rule_id=u.id')
            ->distinct(true)
            ->where('a.admin_id', $admin_id)
            ->field('u.id,u.title,u.name,u.pid')
            ->select();
        $this->rules=node_merge($data);
    }
}